package com.hileto.inf.utils.spring;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.hileto.inf.utils.DBUtils;
import com.hileto.inf.utils.ObjectHelp;
import com.hileto.inf.utils.dao.Entity;
import com.hileto.inf.utils.dao.EntityFactory;
import com.hileto.inf.utils.dao.PageInfo;

/**
 * Copyright: Copyright (c) 2012
 *     深圳市海乐淘电子商务有限公司
 * @Author : 曾林宝
 * @CreateTime : 2013-1-21 下午02:25:02
 *
 * @Description: spring JDBC统一处理类
 */
@SuppressWarnings("all")
public class SpringJdbcProxy {
	
	protected JdbcTemplate jdbcTemplate;
	protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	public SpringJdbcProxy() {
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public void setNamedParameterJdbcTemplate(
			NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	public final JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public final NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}

	/**
	 * @Description : 保存一个实体.
	 * @Author : 曾林宝
	 * @CreateTime : 2013-1-22 下午02:13:45
	 * @param model
	 * @return
	 */
	public final int saveObject(Object model) {
		int result =  -1;
		if(ObjectHelp.isNotEmpty(model)){
			Entity entity = EntityFactory.parseEntity(model.getClass());
			String sql = entity.generateInsertSQL();
			SqlParameterSource paramSource = new BeanPropertySqlParameterSource(model); 
			result = namedParameterJdbcTemplate.update(sql, paramSource);
		}
		return result;
	}

	public int updateObject(Object model) {
		int result =  -1;
		if(ObjectHelp.isNotEmpty(model)){
			Entity entity = EntityFactory.parseEntity(model.getClass());
			String sql = entity.generateUpdateSQL();
			SqlParameterSource paramSource = new BeanPropertySqlParameterSource(model); 
			result = namedParameterJdbcTemplate.update(sql, paramSource);
		}
		return result;
	}
	
	public int deleteObject(Object model) {
		int result =  -1;
		if(ObjectHelp.isNotEmpty(model)){
			Entity entity = EntityFactory.parseEntity(model.getClass());
			String sql = entity.generateDeleteSQL();
			SqlParameterSource paramSource = new BeanPropertySqlParameterSource(model); 
			result = namedParameterJdbcTemplate.update(sql, paramSource);
		}
		return result;
	}
	public <T> int deleteObjectById(Class<T> entityClass, Serializable id) {
		int result =  -1;
		if(ObjectHelp.isNotEmpty(id)){
			Entity entity = EntityFactory.parseEntity(entityClass);
			String sql = entity.generateDeleteSQLByID();
			result = jdbcTemplate.update(sql, id);
		}
		return result;
	}
	
	public <T> T selectObjectById(Class<T> entityClass, Serializable id) {
		T result = null;
		if(ObjectHelp.isNotEmpty(id)){
			Entity entity = EntityFactory.parseEntity(entityClass);
			String sql = entity.generateSelectSQLByID();
			result = (T) jdbcTemplate.queryForObject(sql, entity.getRowMapper(), id);
		}
		return result;
	}

	public <T> List<T> queryForList(Class<T> clazz) {
		List<T> result = null;
		Entity  entity = EntityFactory.parseEntity(clazz);
		String sql = DBUtils.generateSelectSQL(entity.getTableName(), null);
		result = jdbcTemplate.query(sql, entity.getRowMapper());
		return result;
	}
	
	public <T>PageInfo<T> queryForList(PageInfo<T> pageInfo,Class<T> clazz) {
		if(ObjectHelp.isNotEmpty(pageInfo,clazz)){
			Entity  entity = EntityFactory.parseEntity(clazz);
			String baseSql = entity.getSelectSql(null, " order by " + entity.getIdFieldName());
			this.queryForList(pageInfo, clazz, baseSql);
		}
		return pageInfo;
	}
	public <T>PageInfo<T> queryForList(PageInfo<T> pageInfo,Class<T> clazz,String baseSql,Object... args) {
		if(ObjectHelp.isNotEmpty(pageInfo,clazz,baseSql)){
			String sql = pageInfo.proccessSQL(baseSql);
			if(null == pageInfo.getTotalCount()){
				String countSql = DBUtils.getCountSql(sql);
				Long count = null;
				if(ObjectHelp.isEmpty(args)){
					count = jdbcTemplate.queryForLong(countSql,args);
				}else{
					count = jdbcTemplate.queryForLong(countSql,args);
				}
				pageInfo.setTotalCount(count);
			}
			List<T> result = null;
			if(pageInfo.getTotalPages() > 0 && pageInfo.getTotalPages() >= pageInfo.getCurrPageNo()){
				Entity entry = EntityFactory.parseEntity(clazz);
				result = jdbcTemplate.query(sql,entry.getRowMapper(),args);
			}else{
				result = new ArrayList<T>();
			}
			pageInfo.setResult(result);
		}
		return pageInfo;
	}
	public PageInfo<Map<String,Object>> queryForList(PageInfo<Map<String,Object>> pageInfo,String baseSql,Object... args) {
		if(ObjectHelp.isNotEmpty(pageInfo)){
			String sql = pageInfo.proccessSQL(baseSql);
			if(null == pageInfo.getTotalCount()){
				String countSql = DBUtils.getCountSql(sql);
				Long count = jdbcTemplate.queryForLong(countSql,args);
				pageInfo.setTotalCount(count);
			}
			List<Map<String,Object>> result = null;
			if(pageInfo.getTotalPages() > 0 && pageInfo.getTotalPages() >= pageInfo.getCurrPageNo()){
				result = jdbcTemplate.queryForList(sql, args);
			}else{
				result = new ArrayList<Map<String,Object>>();
			}
			pageInfo.setResult(result);
		}
		return pageInfo;
	}
}
